package com.freeman.cement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.freeman.cement.entity.ResponseResult;
import com.freeman.cement.exception.AppException;
import com.freeman.cement.exception.AppExceptionCodeMsg;
import com.freeman.cement.mapper.WorksectionDao;
import com.freeman.cement.entity.WorkSection;
import com.freeman.cement.service.WorksectionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class WorksectionServiceImpl implements WorksectionService {

    //引用类型的自动注入@Autowired,@Resource
    @Resource
    private WorksectionDao worksectionDao;

    @Override
    public ResponseResult selectWorksection(int pageNo, int pageSize) {
        //验证分页范围
        if (pageNo < 1 || pageSize > 1000) {
            throw new AppException(AppExceptionCodeMsg.PAGE_NO_OR_SIZE_WRONG);
        } else {
            PageHelper.startPage(pageNo, pageSize, true);
            List<WorkSection> list = worksectionDao.selectList(null);
            PageInfo<WorkSection> pageInfo = new PageInfo<WorkSection>(list);
            return ResponseResult.success(pageInfo);
        }
    }

    @Override
    public List<WorkSection> selectAllWorksection() {
        return worksectionDao.selectAllWorksection();
    }

    private boolean checekWorksection(String name){
        QueryWrapper<WorkSection> qw = new QueryWrapper<>();
        qw.select("workSection").eq("workSection", name);
        Long a = worksectionDao.selectCount(qw);
        if (a > 0) {
            throw new AppException(AppExceptionCodeMsg.NAME_EXIST);
        }
        return true;
    }

    @Override
    public ResponseResult addWorksection(WorkSection worksection) {
        if(worksection != null && worksection.getWorksection() != null && StringUtils.isNotBlank(worksection.getWorksection())){
            //检查专业名是否重复
            checekWorksection(worksection.getWorksection());
            //存入数据库
            int insert = worksectionDao.insert(worksection);
            if (insert > 0) {
                return ResponseResult.success("新增成功", worksection.getId());
            } else
                throw new AppException(AppExceptionCodeMsg.ADD_WORKSECTION_ERROR);
        } else {
            throw new AppException(AppExceptionCodeMsg.CONTENT_IS_NULL);
        }
    }

    @Override
    public ResponseResult deleteWorksection(int id) {
        if(id > 0){
            WorkSection worksection = worksectionDao.selectById(id);
            if(worksection != null){
                int rslt = worksectionDao.deleteById(id);
                if(rslt > 0){
                    return ResponseResult.success("删除成功", null);
                } else {
                    throw new AppException(AppExceptionCodeMsg.DELETE_WORKSECTION_ERROR);
                }
            } else {
                throw new AppException(AppExceptionCodeMsg.ID_NOT_EXISTS);
            }
        } else {
            throw new AppException(AppExceptionCodeMsg.ID_IS_NULL);
        }
    }

    @Override
    public ResponseResult updateWorksection(WorkSection worksection) {
        if(worksection != null && worksection.getId() != null && worksection.getId() > 0){
            WorkSection old = worksectionDao.selectById(worksection.getId());
            if(old != null){
                if(worksection.getWorksection() != null && StringUtils.isNotBlank(worksection.getWorksection()) && !worksection.getWorksection().equals(old.getWorksection())){
                    checekWorksection(worksection.getWorksection());
                }
                worksection.setId(old.getId());
                int rslt = worksectionDao.updateById(worksection);
                if(rslt > 0){
                    return ResponseResult.success("修改成功", null);
                } else {
                    throw new AppException(AppExceptionCodeMsg.UPDATE_WORKSECTION_ERROR);
                }
            } else {
                throw new AppException(AppExceptionCodeMsg.ID_NOT_EXISTS);
            }
        } else {
            throw new AppException(AppExceptionCodeMsg.ID_IS_NULL);
        }
    }


}
